% Function implementing equality constraints
function y = cloth_eqconstr_4(array,Restlength,D,m,g)

% Convert array to 3d tensor
tensor = array2tensor(array);
N=length(tensor(:,1,1));

% Every point has a penalty for the deviation from the desired value
Aeq=zeros(4,N^2);
Aeq(1,1)=1; 
Aeq(2,N)=1;
Aeq(3,N^2)=1;
Aeq(4,(N^2-(N-1)))=1;
Aeq=kron(Aeq,eye(3));
%Desired values
beq=[-N/2 -N/2 1 -N/2 N/2 1 N/2 N/2 1 N/2 -N/2 1];
y = Aeq*array-beq';